package com.vmonkey.tbkexport;

import android.content.Context;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ProgressBar;


public class ButtonWithProgress extends FrameLayout {

	private Button button;
	private ProgressBar progress;
	public boolean btn_show = true;

	private onButtonClickListener bcListener;

	public interface onButtonClickListener {
		public void onButtonClick();
	}

	public void setOnButtonClickListener(onButtonClickListener bcListener) {
		this.bcListener = bcListener;
	}

	public ButtonWithProgress(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	public ButtonWithProgress(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public ButtonWithProgress(Context context) {
		super(context);
		init();
	}

	private void init() {

		LayoutInflater inflater = LayoutInflater.from(getContext());
		View v = inflater.inflate(R.layout.view_btn_progress, null);
		button = (Button) v.findViewById(R.id.view_button);
		button.setText("Button");

		progress = (ProgressBar) v.findViewById(R.id.view_progress);

		FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
				FrameLayout.LayoutParams.MATCH_PARENT,
				FrameLayout.LayoutParams.MATCH_PARENT);
		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (bcListener != null)
					bcListener.onButtonClick();
			}
		});

		addView(v, params);
	}

	public void setBtnBackground(int id) {
		button.setBackgroundResource(id);
	}

	public void setBtnText(String str) {
		button.setText(str);

	}

	public void setBtnTextColor(int color) {
		button.setTextColor(color);
	}

	public void setBtnTextSize(int size) {
		button.setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
	}

	public void showButton() {
		btn_show = true;

		Animation in = AnimationUtils.loadAnimation(getContext(),
				R.anim.button_in);
		in.setAnimationListener(button_anim);
		button.startAnimation(in);
		Animation out = AnimationUtils.loadAnimation(getContext(),
				R.anim.button_out);
		out.setAnimationListener(progress_anim);
		progress.startAnimation(out);
	}

	public void showProgress() {
		btn_show = false;
		Animation out = AnimationUtils.loadAnimation(getContext(),
				R.anim.button_out);
		out.setAnimationListener(button_anim);
		button.startAnimation(out);
		Animation in = AnimationUtils.loadAnimation(getContext(),
				R.anim.button_in);
		in.setAnimationListener(progress_anim);
		progress.startAnimation(in);
	}

	public void setButton(Button btn) {
		this.button = btn;

	}

	AnimationListener progress_anim = new AnimationListener() {

		@Override
		public void onAnimationStart(Animation arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onAnimationRepeat(Animation arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onAnimationEnd(Animation arg0) {
			if (btn_show)
				progress.setVisibility(View.INVISIBLE);
			else
				progress.setVisibility(View.VISIBLE);
		}
	};

	AnimationListener button_anim = new AnimationListener() {

		@Override
		public void onAnimationStart(Animation arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onAnimationRepeat(Animation arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onAnimationEnd(Animation arg0) {
			if (btn_show)
				button.setVisibility(View.VISIBLE);
			else
				button.setVisibility(View.INVISIBLE);
		}
	};
}
